package com.mdlib.helper.module.send.profile.fragments;

import android.app.Dialog;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.ljlib.help.R;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.ui.adapter.GenericViewPagerAdapter;
import com.mdlib.helper.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：HQY on 16/11/15 17:19
 * 邮箱：hqy_xz@126.com
 */

public class VoucherFragment extends BaseTitleFragment {


    @Bind(R.id.tab_voucher_top)
    TabLayout mTabVoucherTop;
    @Bind(R.id.vp_voucher_pager)
    ViewPager mVpVoucherPager;

    private UnusedVoucherFragment unusedVoucherFragment;

    private HasBeenUsedVoucherFragment hasBeenUsedVoucherFragment;

    private ExpiredVoucherFragment expiredVoucherFragment;

    private FragmentPagerAdapter voucherAdapter;

    private List<Fragment> listFragment;

    private List<String> titlFragment;

    private Dialog mExplainDialog;


    public static VoucherFragment newInstance() {
        return new VoucherFragment();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_voucher;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.voucher_tv_title, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                })
                .setImgRightBg(R.drawable.iv_voucher_explain)
                .setImgRightListener(mVoucherExplainListener);
        init();
    }

    private void init() {

        unusedVoucherFragment = new UnusedVoucherFragment();
        hasBeenUsedVoucherFragment = new HasBeenUsedVoucherFragment();
        expiredVoucherFragment = new ExpiredVoucherFragment();


        listFragment = new ArrayList<>();
        listFragment.add(unusedVoucherFragment);
        listFragment.add(hasBeenUsedVoucherFragment);
        listFragment.add(expiredVoucherFragment);

        titlFragment = new ArrayList<>();
        titlFragment.add(getActivity().getResources().getString(R.string.voucher_tv_title_unused));
        titlFragment.add(getActivity().getResources().getString(R.string.voucher_tv_title_hasbeenused));
        titlFragment.add(getActivity().getResources().getString(R.string.voucher_tv_title_expired));


        mTabVoucherTop.setTabMode(TabLayout.MODE_FIXED);

        mTabVoucherTop.addTab(mTabVoucherTop.newTab().setText(titlFragment.get(0)));
        mTabVoucherTop.addTab(mTabVoucherTop.newTab().setText(titlFragment.get(1)));

        voucherAdapter = new GenericViewPagerAdapter(getActivity().getSupportFragmentManager(), listFragment, titlFragment);
        mVpVoucherPager.setAdapter(voucherAdapter);
        mTabVoucherTop.setupWithViewPager(mVpVoucherPager);


    }

    public View.OnClickListener mVoucherExplainListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mExplainDialog = new Dialog(getActivity(), R.style.dialog);
            mExplainDialog.setContentView(R.layout.dialog_voucher_explain);
            mExplainDialog.setCanceledOnTouchOutside(true);

            TextView mTvExplain = (TextView) mExplainDialog.findViewById(R.id.tv_voucher_explain);
            mTvExplain.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ToastUtil.showToast("代金券说明");
                    mExplainDialog.dismiss();
                }
            });
            mExplainDialog.show();
        }
    };
}
